Skip to content

internal/fakecgo: align the stack for C calls on ppc64le - #573

Merged
hajimehoshi merged 1 commit into
ebitengine:mainfrom
hajimehoshi:claude/ppc64le-call5-align-2d6ec7
Nov 24, 2026
Merged

hajimehoshi merged 1 commit into
ebitengine:mainfrom
hajimehoshi:claude/ppc64le-call5-align-2d6ec7

Conversation

@hajimehoshi

@hajimehoshi hajimehoshi commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

What issue is this addressing?

No issue is filed. This was found by review, and no failure has been reported.

What type of issue is this addressing?

bug

What this PR does | solves

Problem. ELFv2 requires R1 to be 16-byte aligned at a C call, but Go frames on ppc64le are only 8-byte aligned, and fakecgo's call5 only subtracted 32 from R1. Depending on the Go caller's frame size, libc functions ran on a stack that is off by 8. This is latent: no crash was observed. The risk is stvx/lvx, which ignore the low 4 address bits, in C code that uses vector stack slots.

Fix. call5 aligns R1 down to 16 bytes below the caller's frame and restores the original value after the call.

  • The original R1 is kept in R22, which C preserves. R14 is not used because rt0_go keeps its stack pointer there across _cgo_init.
  • The back chain is stored before R1 moves, so 0(R1) is always valid.
  • The 32-byte adjustment stays visible to the assembler, so the nosplit height of call5 is still 32 and the stack check results do not change. The up to 8 bytes of alignment padding are not counted. call5 is now marked SPWRITE by the assembler.
  • setg_trampoline is unchanged. It calls the runtime's setg_gcc, which is Go assembly that makes no C call.

Testing. linux/ppc64le under qemu-user 10.0.13 (Debian 13, Podman), cross-built with Go 1.27.4, default and PIE unless noted. No ppc64le hardware was used.

  • A temporary probe in call5 (not part of this PR) recorded R1&15 right before the C call. It was 0 and 8 without the fix, and only 0 with it. The back chain was always the caller's R1.
  • An LD_PRELOAD library interposed libc functions on unprobed fakecgo test binaries. Without the fix, pthread_attr_init, pthread_attr_getstacksize, sigfillset and pthread_sigmask were entered with R1&15 == 8 while TestThreadEntryReturn ran. With it, all the interposed calls were entered with 0.
  • The root package's suite, run as CI does, passes 10 times, with the same pass and skip counts as without the fix. TestCallbackEntries in child mode passes. The fakecgo tests pass 50 times.
  • With Go 1.25.14, the root and fakecgo test binaries link, and their tests pass.
  • gofmt is clean. go vet reports the same two findings as on main (setg_trampoline, call5).
  • -gcflags=all=-N -l builds still fail to link with the same 16-byte nosplit overflow in x_cgo_thread_start as on main.

The tests above ran on top of 2fc8a24, before #570. After rebasing onto #570, whose changes do not overlap with this one, only the builds, go vet and gofmt were rerun.

No test is added: it would need an assembly callee, and Go has no test-only assembly files.

Filed by Claude (Claude Code), on behalf of @hajimehoshi.

🤖 Generated with Claude Code

ELFv2 requires R1 to be 16-byte aligned at a call, but Go frames on
ppc64le are only 8-byte aligned and call5 only subtracted a constant 32
from R1. The C function could therefore run on a misaligned stack, where
stvx and lvx silently drop the low address bits of vector stack slots.

Align R1 down to 16 bytes below the Go caller's frame, and keep the
original R1 in R22, which C preserves. R14 is not used for this because
rt0_go keeps its stack pointer there across _cgo_init.

Store the back chain before moving R1 so that 0(R1) is valid at any
point, and keep the 32-byte adjustment visible to the assembler so that
the nosplit stack accounting does not change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 24, 2026 04:11
@hajimehoshi

Copy link
Copy Markdown
Member Author

@TotallyGamerJet PTAL

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new ppc64le alignment behavior lacks a regression test covering both incoming stack alignments, the callee’s alignment and back chain, and restoration of the original stack pointer.

1 open issue
What changed in this PR

This PR fixes a latent stack-alignment issue in fakecgo’s ppc64le C-call bridge, bringing call5 into compliance with ELFv2’s 16-byte alignment requirement.

Changes:

  • Aligns the stack before C calls while preserving a valid back chain.
  • Saves the original stack pointer in R22 and restores it before storing the return value.
File Description
internal/​fakecgo/​trampolines_ppc64le.s Updates call5 to align the C-call stack and restore the original stack pointer afterward.

Review effort: Balanced

Comment thread internal/fakecgo/trampolines_ppc64le.s Resolved

@TotallyGamerJet TotallyGamerJet left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@hajimehoshi
hajimehoshi merged commit 9a9f87f into ebitengine:main Nov 24, 2026
27 checks passed
@hajimehoshi
hajimehoshi deleted the claude/ppc64le-call5-align-2d6ec7 branch November 24, 2026 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants